<?php


namespace app\app\controller;


use app\app\model\FriendMessageComments;
use app\app\model\SysFileInfoModel;
use app\app\model\UserModel;
use app\BaseController;
use think\Exception;
use think\facade\Db;

class FriendMessage extends BaseController
{
    /**
     * 新增动态
     */
    public function add()
    {
        $data = $this->request->param();
        $data['update_time'] = date('Y-m-d h:i:s', time());
        $data['create_time'] = date('Y-m-d h:i:s', time());
        $data['time'] = time(); //时间线
        $res = \app\app\model\FriendMessage::create($data);
        if (!$res) {
            $this->error('提交失败');
        }
        $this->success('提交成功');

    }

    /**
     * 新增评论与回复
     */
    public function addComments()
    {
        $data = $this->request->param();
        $data['update_time'] = date('Y-m-d h:i:s', time());
        $data['create_time'] = date('Y-m-d h:i:s', time());
        if (!isset($data['reply_to_id']) && empty($data['reply_to_id'])) {
            $data['reply_to_id'] = 0;  //$data['reply_to_id'] 为0时为回复
        }
        $res = \app\app\model\FriendMessageComments::create($data);
        if (!$res) {
            $this->error('提交失败');
        }
        $this->success('提交成功');

    }

    /**
     * 查询动态
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list()
    {
        $user_id = input('user_id', 0);
        if (empty($user_id)) {
            $this->error('用户id不可为空');
        }
        $page_size = input('page_size', 10);
        $FriendMessageModel = new \app\app\model\FriendMessage();
        $list = $FriendMessageModel->order('time', 'desc')->paginate($page_size);

        $user_data_name = UserModel::column('nick_name', 'user_id');
        $user_data_head = UserModel::column('avatar', 'user_id');
        foreach ($list as $key => $value) {
            //判断是否为当前用户，是的话可删除评论或者动态
            if ($value['user_id'] == $user_id) {
                $list[$key]['is_delete'] = true;
            } else {
                $list[$key]['is_delete'] = false;
            }
            //判断动态是否有图片
            if (!empty($list[$key]['image'])) {
                $list[$key]['image'] = explode(',', $list[$key]['image']);
                $list[$key]['image_path'] = SysFileInfoModel::where('file_id', 'in', $list[$key]['image'])
                    ->column('file_path');
            } else {
                $list[$key]['image_path'] = [];
            }
            $list[$key]['comment'] = FriendMessageComments::where('message_id', $value['id'])
                ->select();
            foreach ($list[$key]['comment'] as $k => $v) {
                $list[$key]['comment'][$k]['user_name'] = $user_data_name[$v['user_id']];
                if ($v['reply_to_id'] != 0) {
                    $list[$key]['comment'][$k]['reply_name'] = $user_data_name[$v['reply_to_id']];
                }
                if ($v['user_id'] == $user_id) {
                    $list[$key]['comment'][$k]['is_delete'] = true;
                } else {
                    $list[$key]['comment'][$k]['is_delete'] = false;
                }
            }
            $list[$key]['user_name'] = $user_data_name[$value['user_id']];
            $list[$key]['avatar'] = $user_data_head[$value['user_id']];
        }
        $this->success('ok', $list);
    }

    /**
     * 删除动态
     */
    public function deleteMessage()
    {
        $id = input('message_id', '');
        if (empty($id)) {
            $this->error('动态id不可为空');
        }
        Db::startTrans();
        try {
            $re[1] = \app\app\model\FriendMessage::where('id', $id)
                ->delete();
            $re[2] = FriendMessageComments::where('message_id', $id)
                ->delete();
            if (in_array('0', $re)) {
                throw new Exception('异常');
            }
            $this->success('删除成功！');
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**
     * 删除回复
     */
    public function deleteComments()
    {
        $id = input('comment_id', '');
        if (empty($id)) {
            $this->error('动态id不可为空');
        }
        $res = FriendMessageComments::where('id', $id)
            ->delete();
        if ($res) {
            $this->success('删除成功！');
        }
        $this->error('删除失败');
    }

}